<?php 
/*-- Project Introduce --*/

/**
 * 控制器类 Login 
 *
 * @access public
 */
class JJ_Login 
	extends JCAT_Controller 
{

	/**
	 * 构造函数 
	 * 
	 * @access	public
	 * @param	$aParent	JCAT_IController	父控制器 
	 * @return	void
	 */
	public function JJ_Login(JCAT_IController $aParent)
	{
		// 调用父类构造函数
		$this->JCAT_Controller(__CLASS__,$aParent) ;
		
		// 初始化文档、视图、子控制器 
		$this->Initialize() ;
	}

	/**
	 * 初始化文档、视图、子控制器 
	 * 
	 * @access	protected
	 * @return	void
	 */
	protected function Initialize()
	{
		// 消息队列
		$aMessageQueue = new JCAT_MessageQueue() ;
		JCAT_MessageDispatcher::RegisterMessageQueue($aMessageQueue,JJ_Login::MSGTYPE_FAIL) ;

		// 消息输出对象
		$aMessageQueue->AddPrinter(new JCAT_MessageGtkDialogPrinter($aMessageQueue)) ;
		$this->SetMessageQueue($aMessageQueue) ;
	}

	
	/**
	 * 执行入口函数 
	 * 
	 * @access	public
	 * @param	$arrArg=array	array	参数 
	 * @return	array(JJ_Account,Jabber)
	 */
	public function MainRun(array $arrArg=array())
	{$arrArg ;
		while(1)
		{
			// 显示视图 LoginDialog
			$aView = new JJ_LoginDialog($this) ;
			$nResponse = $aView->DoDialog() ;

			if( $nResponse===Gtk::RESPONSE_OK )
			{
				JJ::PlaySound('login') ;
				
				//
				$sUsername =  $aView->GetUICtrlExchangeData('SelUsername') ;
				$sPassword =  $aView->GetUICtrlExchangeData('EntPassword') ;
				
				$aAccount = JJ_Account::CreateFromUsername($sUsername) ;

				$aXMPPConn = $this->LoginOperation($aAccount,$sPassword) ;
				if( !$aXMPPConn )
				{
					$aMessageQueue = $this->GetMessageQueue() ;
					$aMessageQueue->PrintQueue() ;
				}
				
				else 
				{
					$aView->SaveUserMemery() ;
					return array($aAccount,$aXMPPConn) ;
				}

			}
			else
			{
				// 退出
				return array(null,null) ;
			}
		}
		
	}
	
	public function LoginOperation(JJ_Account $aAccount,$sPassword,$aXMPPConn=null)
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$sServerHost = $aSetting->GetItem('Jabber','ServerHost') ;
		$sServerPort = $aSetting->GetItem('Jabber','ServerPort') ;
		$nLoginTimeout = $aSetting->GetItem('Jabber','LoginTimeout') ;
		$sRCName = $aSetting->GetItem('JJ','ClientResourceName') ;
		
		if( !$aXMPPConn )
		{
			$aXMPPConn = new Jabber(false) ;
			if( !$aXMPPConn->connect($sServerHost,$sServerPort,$nLoginTimeout) )
			{
				new JCAT_Message( JCAT_Language::Sentence('无法连接到服务器。'), self::MSGTYPE_FAIL ) ;
				return null ;
			}
		}

		$aXMPPConn->set_handler("authenticated",$this,"OnAuthenticated");
		$aXMPPConn->set_handler("authfailure",$this,"OnAuthFailure");
		$aXMPPConn->login($aAccount->GetID(),$sPassword,$sRCName) ;
		
		// 等待 操作结果
		$this->bLooping = true ;
		while($this->bLooping)
		{
			usleep(200000);
			$aXMPPConn->loop_execute_once() ;
		}
		
		// 解除
		// $aXMPPConn->set_handler("authenticated",null,null);
		// $aXMPPConn->set_handler("authfailure",null,null);
	
		// 返回
		if( $this->bSuccess )
		{
			return $aXMPPConn ;
		}
		else 
		{
			new JCAT_Message( JCAT_Language::Sentence('用户名或密码错误。'), self::MSGTYPE_FAIL ) ;
			return null ;
		}
	}	

	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnAuthenticated()
	{
		$this->bLooping = false ;
		$this->bSuccess = true ;
	}
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnAuthFailure()
	{
		$this->bLooping = false ;
		$this->bSuccess = false ;
	}
	
	const MSGTYPE_FAIL = 'LoginFail' ;
}

?>